package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import server.FirewallException;
import server.ReadWriteException;

import server.FirewallSession;

/**
 * Logger window:
 * 		two tables - logger and statistics
 * 		edit options - logger display (iteractive/static), logger history, sorting
 */
public class LoggerFrame extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1938810537384757114L;

	/**
	 * server controller
	 */
	public FirewallSession UI;
	
	/* Current shown window */
	public static LoggerFrame currentloggerFrame = null;

	/* Logger panel */
	private LoggerPanel logTable = null;
	private LoggerGlobalPanel logGlobalTable = null;
	
	/* Previous history depth */
	private int depth = Settings.historyDepth;
	
	/* Button for turnig logger on off */
	private JButton logonoff = null;
	
	/* C'tor */
	private LoggerFrame(FirewallSession UI)
	{	
		super("Logger");
		this.UI = UI;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(Globals.loggerFrameSize);
		setLayout(new BorderLayout());
		
		addStandartButtons();
		addLoggerButtons();
		addLoggerTable();
		
	}
	
	/**
	 * Returns current frame if exists, ales creates it.
	 * @return logger frame
	 */
	public static LoggerFrame getInctance(FirewallSession ui)
	{
		if(currentloggerFrame == null)
		{
			currentloggerFrame = new LoggerFrame(ui);
		}else{
			LoggerTable.currentLoggerTable.build();
			LoggerGlobalTable.currentLoggerGlobalTable.build();
		}
		
		return currentloggerFrame;
	}
	
	/**
	 * 
	 */
	private void addLoggerButtons() 
	{
		JPanel buttons = new JPanel();
		buttons.setLayout(new GridLayout(1,3));
	    add(buttons, BorderLayout.NORTH);
	    
	    
		/* Buttons for mode set */
		Box modeBox = Box.createVerticalBox();
		
		final JButton staticMode = new JButton("Static Mode    ");
		staticMode.setToolTipText("Logger displays last received data");
		staticMode.setEnabled(false);
		final JButton dynamicMode = new JButton("Dynamic Mode");
		dynamicMode.setToolTipText("Logger dynamically displays new data");
		
		modeBox.add(staticMode);
		modeBox.add(dynamicMode);
		buttons.add(modeBox);
	
		/* Buttons for filter settings and restoring */	
		Box optionsBox = Box.createVerticalBox();
	
		JButton sort = new JButton("Filter               ");
		sort.setToolTipText("Filter the logger reports by chosen parameter");
		JButton reset = new JButton("Reset               ");
		reset.setToolTipText("Set the default settings");
		
		optionsBox.add(sort);
		optionsBox.add(reset);
		
		buttons.add(optionsBox);
		
		/* Buttons for reset */
		final JButton logonoff = new JButton("Logger off");
		logonoff.setToolTipText("Turn off the logger");
		Box logBox = Box.createVerticalBox();
		logBox.add(logonoff);
		buttons.add(logBox);
		
		/* Set the default settings */
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Settings.historyDepth = Settings.DEFAULT_LOGGER_HISTORY_DEPTH;
				Settings.currentFilterType = FilterType.ALL;
				LoggerTable.currentLoggerTable.build();
				LoggerGlobalTable.currentLoggerGlobalTable.build();
			}	  
		});
		
		/* Set static mode */
		staticMode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				staticMode.setEnabled(false);
				dynamicMode.setEnabled(true);
				
				LoggerDynamicAction.stop();
			}	  
		});
		
		/* Set dynamic mode */
		dynamicMode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				staticMode.setEnabled(true);
				dynamicMode.setEnabled(false);
				
				new LoggerDynamicAction();
			}	  
		});
	
		/* Open filter window by click */
		sort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				LoggerFilterFrame msg = new LoggerFilterFrame();
				msg.setVisible(true);
			}	  
		});	
		
		/* Open depth window by click */
		logonoff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if(Settings.loggerOn)
				{
					try
					{
						UI.disableLog();
						logonoff.setText("Logger on");
						logonoff.setToolTipText("Turn on the logger");
						Settings.loggerOn = false;
					}
					catch (Exception e)
					{
						ErrorFrame.AddError("Logger can't be turned off", currentloggerFrame);
					} 
				}
				else
				{
					try
					{
						UI.enableLog();
						logonoff.setText("Logger off");
						logonoff.setToolTipText("Turn off the logger");
						Settings.loggerOn = true;
					}
					catch (Exception e)
					{
						ErrorFrame.AddError("Logger can't be turned on", currentloggerFrame);
					}
				}
			}	  
		});
	}
	
	/**
	 * Adds cancel, finish, help buttons.
	 * Builds their listeners.
	 */
	private void addStandartButtons() 
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");
		JButton help = new JButton("Help");
		cancel.setToolTipText("Help for logger screen");
		
		tableBox.add(help);		
		tableBox.add(finish);
		tableBox.add(cancel);
	    
		
		/*Help button listener - closes the frame*/		
		help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				HelpFrame.currentHelpFrame.ShowDesc(Globals.LOGGER);		
			}	  
		});	
		
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if(LoggerDynamicAction.timer != null)
				{
					LoggerDynamicAction.stop();
				}
				LoggerFrame.currentloggerFrame.dispose();				
			}	  
		});
		  
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				LoggerFrame.currentloggerFrame.dispose();
			}	  
		});

	    add(tableBox, BorderLayout.SOUTH);
	}

	/**
	 * Adds table for logger data.
	 */
	private void addLoggerTable()
	{
		JPanel tables = new JPanel();
		tables.setLayout(new BorderLayout());
		
		logTable = new LoggerPanel(UI);
		tables.add(logTable, BorderLayout.NORTH);
		
		JPanel otherTables = new JPanel();
		otherTables.setLayout(new BorderLayout());
		
		logGlobalTable = new LoggerGlobalPanel(UI);
		
		otherTables.add(logGlobalTable, BorderLayout.NORTH);
		
		StatisticsPanel statTable = new StatisticsPanel(UI);		
		otherTables.add(statTable, BorderLayout.SOUTH);
		
		
		tables.add(otherTables, BorderLayout.SOUTH);
		add(tables, BorderLayout.CENTER);
	}
	
	/**
	 * Actions on canel close operation.
	 */
	private void frameClose()
	{
		Settings.historyDepth = depth;
		
		if(LoggerDynamicAction.timer != null)
		{
			LoggerDynamicAction.stop();
		}
	}

	/**
	 * Actions on default close operation.
	 */
	public void dispose()
	{
		frameClose();
		super.dispose();
	}
}
